/**
 * 
 */

package com.shy.portal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.shy.base.dao.impl.BaseDaoImpl;
import com.shy.base.vo.EntityTypeValue;
import com.shy.base.vo.PagerFilter;
import com.shy.base.vo.QueryResult;
import com.shy.portal.dao.RolePermissionDao;
import com.shy.portal.entity.RolePermission;

/**
 * <p>
 * Title: RolePermissionDaoImpl
 * </p>
 * <p>
 * Description:角色权限控制dao层实现
 * </p>
 * <p>
 * Company:ipebg
 * </p>
 * 
 * @author H2013788
 */
@Repository("rolePermissionDao")
public class RolePermissionDaoImpl extends BaseDaoImpl<RolePermission> implements RolePermissionDao {

	/**
     * log
     */
	private Logger logger = LogManager.getLogger(this.getClass());

    @Override
    public PagerFilter getByPager(String whereHql, List<EntityTypeValue> params,
            PagerFilter pagerFilter) {
        Integer firstStart = Integer.parseInt(pagerFilter.getStart() + ""); // 分页开始下标
        Integer showNum = Integer.parseInt(pagerFilter.getMax() + ""); // 显示条数
        QueryResult<RolePermission> result = this.getScrollDataBindName(firstStart, showNum,
                whereHql, params, pagerFilter.getOrderby());
        if (result != null) {
            pagerFilter.setTotal(result.getTotalrecord());
            pagerFilter.setObjs(result.getResultlist());
        }
        return pagerFilter;
    }

    @Transactional
    @Override
    public boolean batchSaveRolePermission(List<RolePermission> list, Integer roleId) {
        try {
            // 刪除原有
            String hql = " delete from RolePermission where role.id=:roleId ";
            Query query = entityManager.createQuery(hql);
            query.setParameter("roleId", roleId);
            query.executeUpdate();
            // 批量添加
            batchSave(list);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("batch save RoleMenu error : " + e);
            throw e;
        }
    }

}
